package com.study.leetcode;

import java.util.Arrays;

public class Solution_167 {
    public static int[] twoSum(int[] numbers, int target) {
        int[] res = new int[2];
        int right = numbers.length - 1;
        int left = 0;
        int sum;
        //题目为有序数组，只需要左右向中间遍历相加就可以知道是否和target相等
        while (right > left) {
            sum = numbers[left] + numbers[right];
            if (sum > target) {
                right--;
            } else if (sum < target) {
                left++;
            } else {
                res[0] = left + 1;
                res[1] = right + 1;
                break;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = {-1, 0};
        int[] ints = twoSum(nums, -1);

        System.out.println("ints = " + Arrays.toString(ints));
    }
}
